package cn.biplam.front.component.data.tree.treebase;

import cn.biplam.common.ajxcode.SystemCode.TreeDcIndexCode;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.SysManageCode.TreeIndexCode;
import cn.biplam.common.opcode.mdManage.TreeCode.TreeOptCode;
import cn.biplam.common.paramters.TreeParam;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.common.wrapper.tree.TreeIndexFields;
import cn.biplam.front.component.AjxResponse;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.HtmlValue;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.htmlscript.script.ScriptCustomer;
import cn.biplam.front.htmlscript.script.ScriptTree;
import cn.biplam.front.htmlscript.script.ScriptPrompt.Close_Prompt;
import cn.biplam.front.template.base.SimpleInput;
import cn.biplam.front.template.base.SimpleTextArea;
import cn.biplam.front.template.control.Confirm;
import cn.biplam.front.template.control.FlatTable;

/**
 * 树维护
 * @author XiaoRuxing 2009-5-21下午09:03:54
 * @version 1.0
 */
public class TreeMantain extends AjxResponse{
	
	/**当前容器的ID */
	public static final String cId = "8111";
	/** 索引表名*/
	private String tableName;
	/** 添加的级联操作operatorId*/
	private String operatorIdAddNew;
	/** 修改的级联操作operatorId*/
	private String operatorIdModify;
	/** 删除的级联操作operatorId*/
	private String operatorIdDelete;
	/** 树的节点类型*/
	private String nodeType;
	/** 索引表字段对象*/
	private TreeIndexFields field;
	/** 调用树的父容器id*/
	private String pcid;
	/** 树VC的行为id*/
	private String aId;
	
	
	
	@Override
	protected HtmlWindow ajxGet() throws ErrorMessage {
		getParam();
		if(actionId.equals(TreeDcIndexCode.Add_Theme)){
			return getAddPanel(mUser,"2");
		}if(actionId.equals(TreeDcIndexCode.Add_Leaf)){
			return getAddPanel(mUser,"3");
		}else if(actionId.equals(TreeDcIndexCode.Edit)){
			return getEditPanel(mUser);
		}else if(actionId.equals(TreeDcIndexCode.Del)){
			return getDelPanel();
		}else if(actionId.equals(TreeDcIndexCode.Add_Opt)){
			return addOpt();
		}else if(actionId.equals(TreeDcIndexCode.Del_Opt)){
			return deleteOpt(); 
		}else if(actionId.equals(TreeDcIndexCode.Edit_Opt)){
			return editOpt();
		}else{
			throw new ErrorMessage("Ajx Exchange outport not found:"+ actionId+" !");
		}
	}
	
	/**
	 * 获取编辑界面
	 * @param mUser 用户
	 * @return 返回业务对象主题内容编辑界面
	 * @throws ErrorMessage
	 */
	private HtmlWindow getEditPanel(UserInfo mUser) throws ErrorMessage{
		getTreeIndexFields();
		
		DataFilter filterIndex=new DataFilter();
		String[] record=recordId.split(",");
		filterIndex.put(field.getNodeId(), record[0]);
		filterIndex.put("table_name", tableName);
		OperatorResult sr=Operator.get(TreeIndexCode.Select_Node, mUser.getUserID(), filterIndex);
		
		HtmlWindow panel=_getPanel(mUser, sr,null);
		ScriptCustomer.getSpSubmitCheckInput(panel,_getThemeEditCheck(),
				cId,TreeDcIndexCode.Edit_Opt,Close_Prompt.Close);
		return panel;
	}

	/**
	 * 获取添加界面
	 * @return 业务对象主题添加界面
	 * @throws ErrorMessage
	 */
	private HtmlWindow getAddPanel(UserInfo mUser,String operatorType) throws ErrorMessage{
		getTreeIndexFields();
		HtmlWindow panel=_getPanel(mUser,null,operatorType);
		ScriptCustomer.getSpSubmitCheckInput(panel,_getThemeEditCheck(),
				cId, TreeDcIndexCode.Add_Opt,Close_Prompt.Close);
		return panel;
	}
	
	/**
	 * 获取添加修改主题界面的检查条件
	 * @return
	 * @throws ErrorMessage 
	 */
	private KvMap<String, String> _getThemeEditCheck() throws ErrorMessage{
		KvMap<String, String> checkField = new KvMap<String, String>();
		checkField.put(TreeParam.NAME,"名称不能为空!");
		return checkField;
	}
	
	/**
	 * 获取删除界面
	 * @return 返回删除界面
	 * @throws ErrorMessage
	 */
	private HtmlWindow getDelPanel() throws ErrorMessage{
		getTreeIndexFields();
		DataFilter filterIndex=new DataFilter();
		String[] record=recordId.split(",");
		filterIndex.put(field.getNodeId(), record[0]);
		filterIndex.put("table_name", tableName);
		OperatorResult sr=Operator.get(TreeIndexCode.Select_Node, mUser.getUserID(), filterIndex);
		
		String themeName=sr.getValue(field.getNodeName());
		String operatorType = sr.getValue(field.getOperatorType());
		String msg="确认删除：\n" + themeName + "?";
		
		Confirm delete =new Confirm(msg,recordId);
		delete.bind(TreeDcIndexCode.Del_Opt);
		
		HtmlWindow win = delete.get();
		win.put(new HtmlValue("h,"+TreeParam.TABLE_NAME,"h,"+TreeParam.TABLE_NAME,tableName));
		win.put(new HtmlValue("h,"+TreeParam.OPERATORID_DELETE,"h,"+TreeParam.OPERATORID_DELETE,operatorIdDelete));
		win.put(new HtmlValue("h,"+TreeParam.NODE_TYPE,"h,"+TreeParam.NODE_TYPE,nodeType));
		win.put(new HtmlValue("h,"+TreeParam.TREE_PCID,"h,"+TreeParam.TREE_PCID,pcid));
		win.put(new HtmlValue("h,"+TreeParam.TREE_AID,"h,"+TreeParam.TREE_AID,aId));
		win.put(new HtmlValue("h,"+TreeParam.OPERATOR_TYPE,"h,"+TreeParam.OPERATOR_TYPE,operatorType));
		
		return win;
	}
	
	/**
	 * 获取业务对象主题添加和编辑表界面
	 * @param mUser 用户 
	 * @return 返回业务对象主题添加和编辑表界面
	 * @throws ErrorMessage
	 */
	private HtmlWindow _getPanel(UserInfo mUser,OperatorResult sr,String operatorType) throws ErrorMessage{
		getTreeIndexFields();
		String name="";
		String desc="";
		
		if(TOOLS.exist(sr)){
			name = sr.getValue(field.getNodeName());
			desc = sr.getValue(field.getNodeDesc());
			operatorType = sr.getValue(field.getOperatorType());
		}
		
		FlatTable table = new FlatTable(cId,recordId);
		table.setWidth("300");
		table.putTh("名称",0,0);
		table.putTd(new SimpleInput(TreeParam.NAME,"30",name).get(), 0, 1);
		table.putTh("描述",1,0);
		table.putTd(new SimpleTextArea(TreeParam.DESC,"30",desc).get(), 1, 1);
		table.setColSize(0, 8);
		
		HtmlWindow et = table.get();
		et.put(new HtmlValue("h,"+TreeParam.TABLE_NAME,"h,"+TreeParam.TABLE_NAME,tableName));
		et.put(new HtmlValue("h,"+TreeParam.OPERATORID_ADD,"h,"+TreeParam.OPERATORID_ADD,operatorIdAddNew));
		et.put(new HtmlValue("h,"+TreeParam.OPERATORID_MODIFY,"h,"+TreeParam.OPERATORID_MODIFY,operatorIdModify));
		et.put(new HtmlValue("h,"+TreeParam.NODE_TYPE,"h,"+TreeParam.NODE_TYPE,nodeType));
		et.put(new HtmlValue("h,"+TreeParam.TREE_PCID,"h,"+TreeParam.TREE_PCID,pcid));
		et.put(new HtmlValue("h,"+TreeParam.TREE_AID,"h,"+TreeParam.TREE_AID,aId));
		et.put(new HtmlValue("h,"+TreeParam.OPERATOR_TYPE,"h,"+TreeParam.OPERATOR_TYPE,operatorType));
		
		return et;
	}
	
	/**
	 * 删除业务对象主题数据操作
	 * @throws ErrorMessage
	 */
	private HtmlWindow deleteOpt() throws ErrorMessage{
		getTreeIndexFields();
		HtmlWindow win=new HtmlDiv();
		DataFilter filter=new DataFilter();
		String[] nodeInfo=recordId.split(",");
		filter.put(field.getNodeId(), nodeInfo[1]);
		filter.putAll(params);
		String[] deleteRefresh = getNodeDeleteInfo(nodeInfo[0]);
		String srcipt=ScriptTree.getTreeRefresh(pcid, aId, deleteRefresh[0],deleteRefresh[1], nodeType);
		Operator.get(TreeOptCode.Delete, mUser.getUserID(), filter);
		win.bind(srcipt);
		return win;
	}
	
	/**
	 * 修改业务对象主题操作
	 * 
	 * @return 刷新业务对象对象管理导航树脚本
	 * @throws ErrorMessage
	 */
	private HtmlWindow editOpt() throws ErrorMessage {
		getTreeIndexFields();
		HtmlWindow win = new HtmlDiv();
		DataFilter filter = new DataFilter();
		String[] nodeInfo = recordId.split(",");
		filter.put(field.getNodeId(), nodeInfo[1]);
		filter.putAll(params);
		Operator.get(TreeOptCode.Update, mUser.getUserID(), filter);
		win.bind(ScriptTree.getTreeRefresh(pcid, aId, nodeInfo[0],getNodeInfo(nodeInfo[0])[0], nodeType));
		return win;
	}
	
	/**
	 * 添加业务对象主题操作
	 * @return  刷新业务对象对象管理导航树脚本
	 * @throws ErrorMessage
	 */
	private HtmlWindow addOpt() throws ErrorMessage{
		getTreeIndexFields();
		String[] record=recordId.split(",");
		HtmlWindow win=new HtmlDiv();
		String parentID=record[1];
		DataFilter filter=new DataFilter();
		filter.putAll(params);
		filter.put(field.getNodeParent(),parentID);
		String nodeID=Operator.get(TreeOptCode.Insert, mUser.getUserID(), filter).getLastRowId();
		win.bind(ScriptTree.getTreeRefresh(pcid, aId, nodeID,getNodeInfo(nodeID)[0], nodeType));
		return win;
	}
	
	/**
	 * 取页面传来的参数
	 */
	private void getParam(){
		tableName = params.getByKey(TreeParam.TABLE_NAME);
		operatorIdAddNew = params.getByKey(TreeParam.OPERATORID_ADD);
		operatorIdModify = params.getByKey(TreeParam.OPERATORID_MODIFY);
		operatorIdDelete = params.getByKey(TreeParam.OPERATORID_DELETE);
		nodeType = params.getByKey(TreeParam.NODE_TYPE);
		pcid = params.getByKey(TreeParam.TREE_PCID);
		aId = params.getByKey(TreeParam.TREE_AID);
	}
	
	/**
	 * 取索引表字段对象
	 * @throws ErrorMessage
	 */
	private void getTreeIndexFields() throws ErrorMessage{
		TOOLS.checkNull(tableName,"TreeMantain.java Error,tableName");
		field = TreeIndexFields.getTreeFields(tableName);
	}
	
	/**
	 * 根据节点id取节点信息
	 * @param nodeId 节点id
	 * @return 节点信息 String[nodeLevel,NodeParent,HaveBrother,HaveLBrother]
	 * @throws ErrorMessage
	 */
	private String[] getNodeInfo(String nodeId) throws ErrorMessage{
		String[] nodeInfo = new String[4];
		DataFilter filter = new DataFilter();
		filter.put(field.getNodeId(), nodeId);
		filter.put("table_name", tableName);
		OperatorResult or = Operator.get(TreeIndexCode.Select_Node, mUser.getUserID(), filter);
		nodeInfo[0]=or.getValue(field.getNodeLevel());
		nodeInfo[1]=or.getValue(field.getNodeParent());
		nodeInfo[2]=or.getValue(field.getHaveBrother());
		nodeInfo[3]=or.getValue(field.getHaveLBrother());
		return nodeInfo;
	}
	
	/**
	 * 删除节点后，需要刷新的节点信息
	 * @param nodeId 节点id
	 * @return 节点信息 String[nodeId,nodeLevel]
	 * @throws ErrorMessage
	 */
	private String[] getNodeDeleteInfo(String nodeId) throws ErrorMessage{
		String[] nodeDelete = new String[2];
		String refreshNodeId = null;
		String[] nodeInfo = getNodeInfo(nodeId);
		if("1".equals(nodeInfo[2]) || "1".equals(nodeInfo[3])){
			DataFilter filter = new DataFilter();
			filter.put(field.getNodeId(), nodeInfo[1]);
			filter.put("table_name", tableName);
			OperatorResult or = Operator.get(TreeIndexCode.Get_Immediate_Children, mUser.getUserID(), filter);
			if(TOOLS.exist(or)){
				refreshNodeId = or.getValue(field.getNodeId());
			}
		}else{
			refreshNodeId = nodeInfo[1];
		}
		nodeDelete[0] = refreshNodeId;
		nodeDelete[1] = getNodeInfo(refreshNodeId)[0];
		return nodeDelete;
	}
}
